package puc.tcc.recomendaeventos;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListaEventos extends ListActivity {

	String[] listaEventos = {};
	ArrayList<String> stringArrayList;
	ContentValues evtValues;
	String idEvtStr;
	Cursor evento;
	BancoEventos db;
	Intent intent;
	int qtd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_eventos);
		
		//criando a string
		db = new BancoEventos(this);
		atualizarLista();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
		atualizarLista();
	}
	
	public void atualizarLista(){
		evento = db.buscar();
		stringArrayList = new ArrayList<String>();
		while(evento.moveToNext()){
			String nome = evento.getString(evento.getColumnIndexOrThrow("nome"));
			stringArrayList.add(nome);
		}
		
		//adaptador da lista de itens
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.linhas, R.id.item, stringArrayList);
	    setListAdapter(adapter);
	}
	
	public void onListItemClick(ListView parent, View view, int position, long id) {
        evento.moveToPosition(position);
        int idEvt = evento.getInt(evento.getColumnIndexOrThrow("_id")); 
        //Toast.makeText(getBaseContext(), "id " + idProd, Toast.LENGTH_LONG).show();
        
        intent = new Intent(this, EvtDetails.class);
        intent.putExtra("_ID", idEvt);
        idEvtStr = Integer.toString(idEvt);
        qtd = evento.getInt(evento.getColumnIndex("quantidade"));
        evtValues = new ContentValues();
        evtValues.put("nome", evento.getString(evento.getColumnIndex("nome")));
        evtValues.put("descricao", evento.getString(evento.getColumnIndex("descricao")));
        evtValues.put("foto", evento.getBlob(evento.getColumnIndex("foto")));
        
        AlertDialog.Builder sobre = new AlertDialog.Builder(this);
		sobre.setPositiveButton("Editar", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                startActivity(intent);
            }
        });
		sobre.setNeutralButton("Comprar", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	if (qtd==0){
            		Toast.makeText(getBaseContext(), "Estoque esgotado! ", Toast.LENGTH_LONG).show();
            	}else{
            		qtd = qtd - 1;
	            	evtValues.put("quantidade", qtd);
	            	db.atualizar(idEvtStr, evtValues);
	            	Toast.makeText(getBaseContext(), "Produto adquirido! ", Toast.LENGTH_LONG).show();
            	}
            }
        });
    	
    	sobre.show();
    }
	
	public void novo(View v){
		Intent intent = new Intent(this, EvtDetails.class);
        intent.putExtra("_ID", 0);
        startActivity(intent);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return false;
	}

}

